package com.jcfk.eam.controller.equ;

import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.controller.parent.EamAssetInfoController;
import com.jcfk.eam.domain.dto.main.EamAssetInfoDTO;
import com.jcfk.eam.domain.query.main.EamAssetInfoQuery;
import com.jcfk.eam.enums.AssetKind;
import com.jcfk.eam.service.equ.EquAssetInfoService;
import com.jcfk.eam.service.main.EamAssetInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 设备台账 服务接口
 *
 * @author libinghai
 * @email 35738025@gqq.com
 * @date 2024-07-28
 */
@Api(tags = {"设备台账"})
@RestController
@RequestMapping("equAssetInfo")
public class EquAssetInfoController {
    @Autowired
    private EamAssetInfoController assetInfoController;

    @Autowired
    private EquAssetInfoService equAssetInfoService;

    @Autowired
    private EamAssetInfoService eamAssetInfoService;

    /**
     * 分页列表
     */
    @ApiOperation("分页查询设备台账")
    @PostMapping("/queryByPage")
    public RetPage<EamAssetInfoDTO> queryByPage(@RequestBody ReqPageParam<EamAssetInfoQuery> requestParam) {
        requestParam.getData().setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        RetPage<EamAssetInfoDTO> pageResult = assetInfoController.queryByPage(requestParam);
        return pageResult;
    }

    /**
     * 列表
     */
    @ApiOperation("查询设备台账")
    @PostMapping("/list")
    public RetResult<List<EamAssetInfoDTO>> list(@RequestBody EamAssetInfoQuery requestParam) {
        requestParam.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        RetResult<List<EamAssetInfoDTO>> result = assetInfoController.list(requestParam);
        return result;
    }


    /**
     * 设备台账
     * 根据id 查询详情
     */
    @ApiOperation("查询设备台账")
    @GetMapping("/get/{id}")
    public RetResult<EamAssetInfoDTO> get(@PathVariable("id") String id) {
        RetResult<EamAssetInfoDTO> itemResult = assetInfoController.get(id);
        return itemResult;
    }

    /**
     * 设备台账
     * 根据设备code 查询详情
     */
    @ApiOperation("根据设备code查询设备台账")
    @GetMapping("/getInfo/{code}")
    public RetResult<EamAssetInfoDTO> getInfobyCode(@PathVariable("code") String code) {
        EamAssetInfoDTO eamAssetInfoDTO = eamAssetInfoService.getInfobyCode(code);
        return RetResult.ok(eamAssetInfoDTO);
    }

    /**
     * 添加/更新管理设备台账
     */
    @ApiOperation("保存设备台账")
    @PostMapping("/save")
    @RequestLogHeader(modular = "设备台账管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamAssetInfoDTO eamAssetInfo) {
        eamAssetInfo.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        // 数据校验
        RetResult ret = equAssetInfoService.isValid(eamAssetInfo);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }
        RetResult<?> result = assetInfoController.save(eamAssetInfo);
        return result;
    }

    /**
     * 批量删除设备台账
     */
    @ApiOperation("批量删除设备台账")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "设备台账管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamAssetInfoDTO eamAssetInfo) {
        RetResult<?> result = assetInfoController.delete(eamAssetInfo);
        return result;
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody EamAssetInfoQuery requestParam, HttpServletResponse response) throws Exception {
        requestParam.setAssetKind(AssetKind.PRODUCTION_EQUIPMENT.getValue());
        assetInfoController.export(requestParam, response);
    }

//    /**
//     * 导入资产台账
//     */
//    @PostMapping("upload")
//    public RetResult<?> upload(MultipartFile file) throws IOException {
//        // 读取excel
//        List<EamAssetInfoUploadExcel> items = EasyExcel.read(file.getInputStream(),EamAssetInfoUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
//        eamAssetInfoService.insertBatch(items);
//        return RetResult.ok();
//    }
//
//    /**
//     * 下载模板
//     */
//    @PostMapping("exportTemp")
//    @ApiOperation("下载模板")
//    public void exportTemp(HttpServletResponse response) throws Exception {
//        List<EamAssetInfoUploadExcel> list = new ArrayList<>();
//        list.add(new EamAssetInfoUploadExcel());
//        ExcelUtils.exportExcelToTarget(response, null, list, EamAssetInfoUploadExcel.class);
//    }


    /**
     * 删除设备台账
     */
    @ApiOperation("删除设备台账")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "资产台账管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        RetResult<?> result = assetInfoController.deleteItem(id);
        return result;
    }
}
